import {
  get,
  post,
  put,
  del,
} from "./base_service"


/**
 * 新平台相关接口
 *
 * allen  2021-08-24
 */




/**
 * 平台发布房源
 * @param {*} data 
 */
const publishPlatformHouse = (data) => {
  return new Promise((resolve, reject) => {
    post("/new/house/manager/push/temp/house", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 新的房源列表
 */
const queryNewHouseList = (page, pageSize, words) => {
  return new Promise((resolve, reject) => {
    get("/new/house/manager/temp/house/list?page=" + page + "&pageSize=" + pageSize + "&words=" + words).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 删除一个房源
 */
const delectOneNewHouse = (id) => {
  return new Promise((resolve, reject) => {
    del("/new/house/manager/del/temp/house?id=" + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}




/**
 * 平台开通会员 (公司和经纪人的)
 */
const openMemberPay = (payType, id) => {
  return new Promise((resolve, reject) => {
    get('/pay?payType=' + payType + "&id=" + id).
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      })
  })
}

/**
 * 平台会员续费 (公司和经纪人的)
 */
const memberRenewAction = () => {
  return new Promise((resolve, reject) => {
    get('/pay/renew').then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    })
  })
}

/**
 * 退款操作
 * @param {*} orderNumber 
 */
const payRefundAction = (orderNumber) => {
  return new Promise((resolve, reject) => {
    get('/pay/refund?orderNumber=' + orderNumber).
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      })
  })
}


/**
 * 查询公司成员详情
 */
const queryCompanyMemberDetail = () => {
  return new Promise((resolve, reject) => {
    get('/user/cash?money=' + money).
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      })
  })
}

/**
 * 获取经纪人入驻管理列表
 */
const queryAgentEnterList = (examineStatus, page, pageSize, words) => {
  return new Promise((resolve, reject) => {
    get("/join/agent/list?examineStatus=" + examineStatus + "&page=" + page + "&pageSize=" + pageSize + "&words=" + words).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 获取置业顾问入驻管理列表
 */
const queryAdviserEnterList = (examineStatus, page, pageSize, words) => {
  return new Promise((resolve, reject) => {
    get("/join/adviser/list?examineStatus=" + examineStatus + "&page=" + page + "&pageSize=" + pageSize + "&words=" + words).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 根据id查询审核经纪人详情
 * @param {*} id 
 */
const queryCheckAgentDetail = (id) => {
  return new Promise((resolve, reject) => {
    get("/join/agent/index?id=" + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 根据id查询审核置业顾问详情
 * @param {*} id 
 */
const queryCheckAdviserDetail = (id) => {
  return new Promise((resolve, reject) => {
    get("/join/adviser/index?id=" + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 获取公司入驻管理列表
 */
const queryCompanyEnterList = (examineStatus, page, pageSize, words) => {
  return new Promise((resolve, reject) => {
    get("/join/company/list?examineStatus=" + examineStatus + "&page=" + page + "&pageSize=" + pageSize + "&words=" + words).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 获取公司审核详情
 */
const queryCompanyCheckDetail = (id) => {
  return new Promise((resolve, reject) => {
    get("/join/company/index?id=" + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 经纪人入驻审核
 * @param {*} data 
 */
const checkAgentEnterAction = (agentId, isPass, remark) => {
  return new Promise((resolve, reject) => {
    get("/join/examine/agent?agentId=" + agentId + "&isPass=" + isPass + "&remark=" + remark).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 置业顾问入驻审核
 * @param {*} data 
 */
const checkAdviserEnterAction = (adviserId, isPass, remark) => {
  return new Promise((resolve, reject) => {
    get("/join/examine/adviser?adviserId=" + adviserId + "&isPass=" + isPass + "&remark=" + remark).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 公司入驻审核
 * @param {*} data 
 */
const checkCompanyEnterAction = (companyId, isPass, remark) => {
  return new Promise((resolve, reject) => {
    get("/join/examine/company?companyId=" + companyId + "&isPass=" + isPass + "&remark=" + remark).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}



/**
 * 获取开通公司或者会员的支付陪住
 * 1:经纪人
 * 2:公司
 * @param {*} type 
 */
const memberPaySetting = (type) => {
  return new Promise((resolve, reject) => {
    get("/pay/setting?type=" + type).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


/**
 * 经纪人入驻房源接口
*/
const agentEnterNewHouse = (houseNumber) => {
  return new Promise((resolve, reject) => {
    get("/pay/setting?houseNumber=" + houseNumber).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


// 佣金规则增删查改 ==========================

/**
 * 查询一个佣金规则详情
*/
const queryOneCommissionRuleDetail = (id) => {
  return new Promise((resolve, reject) => {
    get("/commission/rule/detail?id=" + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 创建一个佣金规则
*/
const createOneCommissionRule = (data) => {
  return new Promise((resolve, reject) => {
    post("/commission/rule/create", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 更新一个佣金规则
*/
const updateOneCommissionRule = (data) => {
  return new Promise((resolve, reject) => {
    put("/commission/rule/update", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 删除一个佣金规则
*/
const deleteOneCommissionRule = (id) => {
  return new Promise((resolve, reject) => {
    del("/commission/rule/del?id=" + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 查询我公司的佣金规则列表
*/
const queryMyCommissionRuleList = () => {
  return new Promise((resolve, reject) => {
    get("/commission/rule/list").then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}






// 佣金规则增删查改 ==========================


/**
 * 经纪人申请入驻为房源专家
 * @param {*} data 
 */
const agentApplyHouseExpert = (data) => {
  return new Promise((resolve, reject) => {
    post("/expert/apply/expert", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 经纪人取消该房源的房源专家身份
 * @param {*} houseNumber 
 */
const agentCancelHouseExpert = (houseNumber, id) => {
  return new Promise((resolve, reject) => {
    del("/expert/cancel/expert?houseNumber=" + houseNumber + "&id=" + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


/**
 * 公司申请房源的佣金入驻
 * @param {*} data 
 */
const companyApplyHouseCommissionJoin = (data) => {
  return new Promise((resolve, reject) => {
    post("/commission/apply/join", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


/**
 * 公司佣金审核列表
 * @param {*} examineStatus 
 * @param {*} page 
 * @param {*} pageSize 
 * @param {*} words 
 */
const companyCommissionCheckList = (examineStatus, page, pageSize, words) => {
  return new Promise((resolve, reject) => {
    get("/manager/commission/examine/list?examineStatus=" + examineStatus + "&page=" + page + "&pageSize=" + pageSize + "&words=" + words).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


/**
 * 根据id查询公司佣金回填
 * @param {*} id 
 */
const companyCommissionBackFill = (id) => {
  return new Promise((resolve, reject) => {
    get("/manager/commission/commission/detail?id=" + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


/**
 * 佣金审核操作
 * @param {*} id 
 * @param {*} isPass 
 * @param {*} remark 
 */
const companyCommissionCheck = (id, isPass, remark) => {
  return new Promise((resolve, reject) => {
    get("/manager/commission/examine?id=" + id + "&isPass=" + isPass + "&remark=" + remark).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 更新公司房源佣金设置
 * @param {*} data 
 */
const updateCompanyHouseCommission = (data) => {
  return new Promise((resolve, reject) => {
    put("/commission/update/apply/join", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 是否开启公司佣金报备
 * @param {*} id 
 */
const isPushCompanyCommission = (id) => {
  return new Promise((resolve, reject) => {
    get("/manager/commission/ispush/commission?id=" + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 根据houseNumber查询当前房源已有的代理公司
 * @param {*} houseNumber 
 */
const queryHouseAgentCompany = (houseNumber) => {
  return new Promise((resolve, reject) => {
    get("/commission/query/apply/company?houseNumber=" + houseNumber).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


/**
 * 设置公司代理房源的对应驻场
 * @param {*} data 
 * 
 * {
    "employeeIds": [],
    "houseNumber": ""
   }
 */
const setCompanyHouseStationRequest = (data) => {
  return new Promise((resolve, reject) => {
    post("/expert/set/resident", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 查询房源代理公司的驻场员工
 * @param {*} companyNumber 
 * @param {*} houseNumber 
 */
const queryCompanyHouseResident = (companyNumber, houseNumber) => {
  return new Promise((resolve, reject) => {
    get("/expert/query/resident?companyNumber=" + companyNumber + "&houseNumber=" + houseNumber).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 经纪人邀请入驻二维码
 */
const queryInviteAgentQrCode = () => {
  return new Promise((resolve, reject) => {
    get("/qr/code/invite/agent/join").then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


/**
 * 公司邀请入驻二维码
 */
const queryInviteCompanyQrCode = () => {
  return new Promise((resolve, reject) => {
    get("/qr/code/invite/company/join").then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


/**
 * 已入驻平台公司列表 
 */
const getCompanyProxyList = (words, page, pageSize) => {
  return new Promise((resolve, reject) => {
    get("/join/company/proxy/list?words=" + words + "&page=" + page + "&pageSize=" + pageSize).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 查询平台公司已入驻房源
 */
const getCompanyEnterHouse = (companyNumber) => {
  return new Promise((resolve, reject) => {
    get("/new/house/proxy/zone?companyNumber=" + companyNumber + "&page=0" + "&pageSize=100").then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}




























module.exports = {
  publishPlatformHouse,//平台发布房源
  openMemberPay,//开通会员
  memberRenewAction,//会员续费操作
  payRefundAction,//退款操作
  queryCompanyMemberDetail,//查询公司成员详情
  queryAgentEnterList,//经纪人入驻管理列表
  queryCompanyEnterList,//公司入驻管理列表
  checkAgentEnterAction,//经纪人入驻审核
  checkCompanyEnterAction,//公司入驻审核
  memberPaySetting,//开通会员支付配置
  queryCheckAgentDetail,//根据id查询经纪人详情
  queryCompanyCheckDetail,//根据id查询公司审核详情
  queryNewHouseList,//查询新的房源列表
  delectOneNewHouse,//删除一个房源
  agentEnterNewHouse,//经纪人入驻绑定房源

  queryOneCommissionRuleDetail,//查询一个佣金规则详情
  createOneCommissionRule,//创建一个佣金规则
  updateOneCommissionRule,//更新一个佣金规则
  deleteOneCommissionRule,//删除一个佣金规则
  queryMyCommissionRuleList,//查询我的佣金规则列表

  agentApplyHouseExpert,//经纪人入驻为房源专家
  agentCancelHouseExpert,//经纪人取消房源专家身份

  companyApplyHouseCommissionJoin,//公司申请房源的佣金入驻
  companyCommissionCheckList,//公司佣金审核列表
  companyCommissionBackFill,//佣金详情回填
  companyCommissionCheck,//佣金审核操作
  updateCompanyHouseCommission,//更新公司佣金设置
  isPushCompanyCommission,//公司房源佣金开启和关闭

  queryHouseAgentCompany,//查询房源已有的代理公司
  setCompanyHouseStationRequest,//设置公司代理房源的对应驻场
  queryCompanyHouseResident, //查询房源代理公司的驻场员工


  queryInviteAgentQrCode,//经纪人邀请二维码
  queryInviteCompanyQrCode,//公司邀请二维码

  queryAdviserEnterList,
  queryCheckAdviserDetail,
  checkAdviserEnterAction,
  getCompanyProxyList,// 查询平台公司
  getCompanyEnterHouse,// 查询平台公司已入驻房源
}
